<?php


class Post_Applies_model extends CI_Model {

    //put your code here
    public $table = "";
    
    function __construct() {
        parent::__construct();
        $this->table = MYSQL_TABLE_PREFLIX.TABLE_USER3_POST_APPLIES;
    }

    function Insert($data){
        $this->db->insert($this->table, $data);   
        $this->AddNotification($data);
    }
    function Check($data){
        $query = $this->db->get_where($this->table,$data);
        if($query->num_rows){
            return true;
        }
        else{
            return false;
        }
    }
   
    function Get_Num($data){
        return $this->db->where($data)->count_all_results($this->table);
    }
    
    function Get_Advanced($select = '*', $where = array(), $order_by = "", $limit = 10, $offset = 0){
        $query = $this->db->select($select)->order_by($order_by)->get_where($this->table, $where,$limit, $offset);
        return $query->result_array();
    }
    
    function Get($data){
        $query = $this->db->get_where($this->table, $data);
        return $query->result_array();
    }
    
    function Delete($data){
        $this->db->delete($this->table, $data);
    }
    
    function AddNotification($data){
        $this->load->model('Posts_model','Posts');
        $this->load->model('Users3_model','Us3');
        $this->load->model('Users2_Notification_model','Us2Noti');
        //Get Post
        $Post = $this->Posts->Get(array('PostID'  =>  $data['RelPostID']));
        $Post = $Post[0];
        //Get Applicant Info
        $Applicant = $this->session->userdata('UserInfo');
        $Applicant = $this->Us3->GetInformation(array('RelUserEmail'   => $Applicant['UserEmail']));
        $Applicant = $Applicant[0];
        //Prepare content
        $Content = "<b>".$Applicant['UserFullname']."</b>"." has applied to your project <b>".$Post['Subject']."</b> ";
        $AppearTime = date('Y-m-d H:i:s');
        
        //Add Notification
        $this->Us2Noti->Insert(array(
            'RelUserEmail'  =>  $Post['RelUserEmail'],
            'Content'       =>  $Content,
            'AppearTime'    =>  $AppearTime,
            'RelPostID'     =>  $Post['PostID'],
            'PictureURL'    =>  $Applicant['AvatarLink'],
            'Type'          =>  'Apply'
        ));
        //Get max ID
        $max_id = $this->Us2Noti->Get_Max();
        $this->Us2Noti->Update(
                array('LinkTo'  =>  'user_group2/notification/' . $max_id . '?Additional[]=' . urlencode($Applicant['RelUserEmail'])), 
                array('NotiID'  => $max_id));
    }
    
    function Get_Day_Applies($data){
        $PAs = $this->Get_Advanced("*", $data, 'ApplyDate desc');
        $Array_Dates = array();
        $PAsTemp = array();
        foreach($PAs as $PA){
            $PA['ApplyDate'] = date("m/d/Y",strtotime($PA['ApplyDate']));
            $PAsTemp[] = $PA;
            if(!in_array($PA['ApplyDate'], $Array_Dates)){
                $Array_Dates[] = $PA['ApplyDate'];
            }
        }
        
        $Result = array();
        $Result['Array_Dates']  = array();
        $Result['Array_Num']    = array();
        foreach($Array_Dates as $Array_Date){
            $i = 0;
            foreach($PAsTemp as $PA){
                if($PA['ApplyDate'] == $Array_Date){
                    $i++;
                }
            }
            $Result['Array_Dates'][]    = $Array_Date;
            $Result['Array_Num'][]      =  $i;
        }
        return $Result;
    }
}
